import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;


public class MemoryStorage implements Storage {
	private List<ByteArrayOutputStream> streams;
	
	public MemoryStorage(int numStreams, int size) {
		this.streams = new java.util.ArrayList<ByteArrayOutputStream>(numStreams);
		for (int i=0; i<numStreams; i++) {
			streams.add(i, new ByteArrayOutputStream(size));
		}
	}

	@Override
	public void reset() throws IOException {
		for (ByteArrayOutputStream out : streams) {
			out.reset();
		}
	}

	@Override
	public OutputStream getOutputStream(int num) {
		return streams.get(num);
	}
	
	@Override
	public InputStream getInputStream(int num) {
		byte[] bytes = streams.get(num).toByteArray();
		return new ByteArrayInputStream(bytes);
	}
	
	public long bitLength(int num) {
		return streams.get(num).toByteArray().length * 8;
	}

}
